﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Letter
{
    public partial class Add : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID;//暂时写死
            string strUserIdFromCookie = Common.Cookie.getCookie("UserID");
            if (string.IsNullOrEmpty(strUserIdFromCookie))
            {
                Response.Redirect("/");
            }
            userID = int.Parse(strUserIdFromCookie);

            string strFrdID = Request.Form["selFriend"];
            string strTitle = Request.Form["txtTitle"];
            string strContent = Request.Form["txtContent"];
            if (!string.IsNullOrEmpty(strFrdID) && !string.IsNullOrEmpty(strTitle) && !string.IsNullOrEmpty(strContent))
            {
                AddLetter(userID, strFrdID, strTitle, strContent);
                Response.Redirect("Out.aspx");
            }
            StringBuilder sbHtml = new StringBuilder();
            sbHtml = CommonHtml(sbHtml, "user_letter_add");//公共部分的html
            sbHtml.Replace("$FriendOption$",FriendList(userID));
            sbHtml.Replace("$FaceList$", new Jy123.BLL.User.UserSql().FaceList(Cache["user_face"].ToString()));//初始化表情列表
            Response.Write(sbHtml.ToString());
        }
        /// 公共部分的html
        /// </summary>
        /// <param name="sbHtml">最终写出页面的大StringBuilder</param>
        /// <param name="cacheHtml">缓存名称</param>
        /// <returns>StringBuilder</returns>
        private StringBuilder CommonHtml(StringBuilder sbHtml, string cacheHtml)
        {
            sbHtml = new StringBuilder(Cache[cacheHtml].ToString());
            sbHtml = sbHtml.Replace("$Header$", Cache["user_header"].ToString());//头部
            sbHtml = sbHtml.Replace("$Footer$", Cache["user_footer"].ToString());//底部
            sbHtml = sbHtml.Replace("$LeftUserCmd$", Cache["user_navigation_up"].ToString());//左侧导航
            sbHtml = sbHtml.Replace("$LeftUserFriendAdd$", Cache["user_friendadd"].ToString());//左侧直接添加好友
            return sbHtml;
        }
        
        /// <summary>
        /// 好友下拉表
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <returns>字符串</returns>
        private string FriendList(int userID)
        {
            List<int?> count;
            StringBuilder sbFrdSelect = new StringBuilder();
            string strFrdSelect = Cache["user_gift_friendselectoption"].ToString();
            DataTable dtFrdList = new Jy123.BLL.User.Friend().GetFriendList(1, int.MaxValue, userID, out count).Tables[0];
            for (int i = 0; i < dtFrdList.Rows.Count; i++)
            {
                string str = strFrdSelect;
                str = str.Replace("$FFriend$", dtFrdList.Rows[i]["FFriend"].ToString())
                    .Replace("$UserName$", dtFrdList.Rows[i]["UserName"].ToString());
                sbFrdSelect.Append(str);
            }
            return sbFrdSelect.ToString();
        }
        /// <summary>
        /// 发送信件
        /// </summary>
        /// <param name="userID">用户ID</param>
        /// <param name="strFrdID">好友的ID</param>
        /// <param name="strTitle">标题</param>
        /// <param name="strContent">内容</param>
        /// <returns>新记录的ID</returns>
        private int AddLetter(int userID, string strFrdID, string strTitle, string strContent)
        {
            Jy123.Model.User_Main mSender = new Jy123.Model.User_Main();
            Jy123.Model.User_Main mReceiver = new Jy123.Model.User_Main();
            Jy123.BLL.User_Main bllUser = new Jy123.BLL.User_Main();
            mSender = bllUser.GetModel(userID);
            mReceiver = bllUser.GetModel(Convert.ToInt32(strFrdID));

            Jy123.Model.User_Letter mLetter = new Jy123.Model.User_Letter();
            Jy123.BLL.User_Letter bllLetter = new Jy123.BLL.User_Letter();
            mLetter.FUserSender = userID;
            mLetter.SenderName = mSender.UserName;
            mLetter.FUserReceiver = Convert.ToInt32(strFrdID);
            mLetter.ReceiverName = mReceiver.UserName;
            mLetter.Title = strTitle;
            mLetter.LetterContent = strContent;
            mLetter.PostTime = DateTime.Now;
            mLetter.PostIP = Common.Public.GetClientIP();
            mLetter.TopicID = 0;
            mLetter.LtType = 1;
            mLetter.IsRead = 0;
            mLetter.IsReply = false;
            mLetter.IsDel = 0;
            int newLetterID = bllLetter.Add(mLetter);
            return newLetterID;
        }
    }
}
